//
//  BCOrderVC.m
//  BangcaiSale
//
//  Created by lsy on 16/7/7.
//  Copyright © 2016年 Bangcai. All rights reserved.
//

#import "BCOrderVC.h"
#import "BCNewOrdDetailVC.h"
#import "BCReadyDetailVC.h"
#import "BCAlreadyDetailVC.h"
#import "BCOrderNewCollectionViewCell.h"
#import "BCOrderWaitCollectionViewCell.h"
#import "BCOrderSendCollectionViewCell.h"

@interface BCOrderVC () <UIScrollViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
//订单种类选择视图
@property (nonatomic, strong) UIView *segmentView;
//订单种类选择按钮背景
@property (nonatomic, strong) UIView *segmentItem_0_BackgroundView;
@property (nonatomic, strong) UIView *segmentItem_1_BackgroundView;
@property (nonatomic, strong) UIView *segmentItem_2_BackgroundView;
//订单种类选择按钮
@property (nonatomic, strong) UIButton *segmentItem_0;
@property (nonatomic, strong) UIButton *segmentItem_1;
@property (nonatomic, strong) UIButton *segmentItem_2;
//三订单滚动视图
@property (nonatomic, strong) UIScrollView *mainScrollView;
//三订单背景视图
@property (nonatomic, strong) UIView *aboveScroll_0_View;
@property (nonatomic, strong) UIView *aboveScroll_1_View;
@property (nonatomic, strong) UIView *aboveScroll_2_View;
//三订单列表视图
@property (nonatomic, strong) UICollectionView *orderList_0_CollectionView;
@property (nonatomic, strong) UICollectionView *orderList_1_CollectionView;
@property (nonatomic, strong) UICollectionView *orderList_2_CollectionView;
//三订单滚动页
@property (nonatomic, assign) NSInteger page;
@end

@implementation BCOrderVC

-(void)viewDidLoad {
    [super viewDidLoad];
    [self p_initNavgationBar];
    [self p_initSegmentView];
    [self p_initMainScroll];
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSNumber *isFromMianPage = [[NSUserDefaults standardUserDefaults] valueForKey:@"isFromMianPage"];
    if (isFromMianPage.boolValue) {
        [self fromMianPage];
    }
}

#pragma mark - View init
- (void)p_initNavgationBar {
    self.title = @"订单";
    UIImage *rightBarItemImage = [UIImage imageNamed:@"上线"];
    UIButton *rightBarButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBarButton setBackgroundImage:rightBarItemImage forState:UIControlStateNormal];
    [rightBarButton setTitle:@"我要接单" forState:UIControlStateNormal];
    rightBarButton.titleLabel.font = [UIFont boldSystemFontOfSize:13];
    rightBarButton.frame = CGRectMake(0, 0, 72, 22);
    [rightBarButton addTarget:self action:@selector(rightItemAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBarButton];
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)p_initSegmentView {
    UIFont *segmentFont = [UIFont boldSystemFontOfSize:15];
    self.segmentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44.f)];
    self.segmentView.backgroundColor = [UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1];
    [self.view addSubview:self.segmentView];
    self.segmentItem_0_BackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.segmentView.frame.size.width/3, self.segmentView.frame.size.height)];
    self.segmentItem_0_BackgroundView.backgroundColor = BangcaiBlue;
    self.segmentItem_0_BackgroundView.hidden = NO;
    self.segmentItem_0 = [UIButton buttonWithType:UIButtonTypeCustom];
    self.segmentItem_0.frame = CGRectMake(0, 0, self.segmentItem_0_BackgroundView.frame.size.width, self.segmentItem_0_BackgroundView.frame.size.height-2.5f);
    [self.segmentItem_0 setTitle:@"新订单" forState:UIControlStateNormal];
    [self.segmentItem_0.titleLabel setFont:segmentFont];
    [self.segmentItem_0 setTitleColor:[UIColor colorWithRed:0.365 green:0.365 blue:0.365 alpha:1.000] forState:UIControlStateNormal];
    [self.segmentItem_0 setBackgroundColor:[UIColor whiteColor]];
    self.segmentItem_0.tag = 0;
    [self.segmentItem_0 addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.segmentView addSubview:self.segmentItem_0_BackgroundView];
    [self.segmentView addSubview:self.segmentItem_0];
    
    self.segmentItem_1_BackgroundView = [[UIView alloc] initWithFrame:CGRectMake(self.segmentItem_0_BackgroundView.frame.size.width, 0, self.segmentView.frame.size.width/3, self.segmentView.frame.size.height)];
    self.segmentItem_1_BackgroundView.backgroundColor = BangcaiBlue;
    self.segmentItem_1_BackgroundView.hidden = YES;
    self.segmentItem_1 = [UIButton buttonWithType:UIButtonTypeCustom];
    self.segmentItem_1.frame = CGRectMake(self.segmentItem_0_BackgroundView.frame.size.width, 0, self.segmentItem_1_BackgroundView.frame.size.width, self.segmentItem_1_BackgroundView.frame.size.height-2.5f);
    [self.segmentItem_1 setTitle:@"待发货" forState:UIControlStateNormal];
    [self.segmentItem_1.titleLabel setFont:segmentFont];
    [self.segmentItem_1 setTitleColor:[UIColor colorWithRed:0.365 green:0.365 blue:0.365 alpha:1.000] forState:UIControlStateNormal];
    [self.segmentItem_1 setBackgroundColor:[UIColor whiteColor]];
    self.segmentItem_1.tag = 1;
    [self.segmentItem_1 addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.segmentView addSubview:self.segmentItem_1_BackgroundView];
    [self.segmentView addSubview:self.segmentItem_1];
    
    self.segmentItem_2_BackgroundView = [[UIView alloc] initWithFrame:CGRectMake(self.segmentItem_0_BackgroundView.frame.size.width*2, 0, self.segmentView.frame.size.width/3, self.segmentView.frame.size.height)];
    self.segmentItem_2_BackgroundView.backgroundColor = BangcaiBlue;
    self.segmentItem_2_BackgroundView.hidden = YES;
    self.segmentItem_2 = [UIButton buttonWithType:UIButtonTypeCustom];
    self.segmentItem_2.frame = CGRectMake(self.segmentItem_0_BackgroundView.frame.size.width*2, 0, self.segmentItem_2_BackgroundView.frame.size.width, self.segmentItem_2_BackgroundView.frame.size.height-2.5f);
    [self.segmentItem_2 setTitle:@"已发货" forState:UIControlStateNormal];
    [self.segmentItem_2.titleLabel setFont:segmentFont];
    [self.segmentItem_2 setTitleColor:[UIColor colorWithRed:0.365 green:0.365 blue:0.365 alpha:1.000] forState:UIControlStateNormal];
    [self.segmentItem_2 setBackgroundColor:[UIColor whiteColor]];
    self.segmentItem_2.tag = 2;
    [self.segmentItem_2 addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.segmentView addSubview:self.segmentItem_2_BackgroundView];
    [self.segmentView addSubview:self.segmentItem_2];
    
    UIView *middleLineView_0 = [[UIView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-4.f)/3, (_segmentView.frame.size.height-16.f)*0.5f, 1.f, 16.f)];
    middleLineView_0.backgroundColor = [UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1];
    [self.segmentView addSubview:middleLineView_0];
    UIView *middleLineView_1 = [[UIView alloc] initWithFrame:CGRectMake(((SCREEN_WIDTH-4.f)/3)*2, (_segmentView.frame.size.height-16.f)*0.5f, 1.f, 16.f)];
    middleLineView_1.backgroundColor = [UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1];
    [self.segmentView addSubview:middleLineView_1];
}

- (void)p_initMainScroll {
    self.mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.segmentView.frame.size.height, self.view.frame.size.width, self.view.frame.size.height-self.segmentView.frame.size.height-50.f-60.f)];
    self.mainScrollView.delegate = self;
    self.mainScrollView.backgroundColor = [UIColor grayColor];
    self.mainScrollView.showsHorizontalScrollIndicator = NO;
    
    self.aboveScroll_0_View = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.mainScrollView.frame.size.width, self.mainScrollView.frame.size.height)];
    self.aboveScroll_0_View.backgroundColor = [UIColor whiteColor];
    [self.mainScrollView addSubview:self.aboveScroll_0_View];
    [self p_initMainScrollFirstView];
    
    self.aboveScroll_1_View = [[UIView alloc] initWithFrame:CGRectMake(self.mainScrollView.frame.size.width, 0, self.mainScrollView.frame.size.width, self.mainScrollView.frame.size.height)];
    self.aboveScroll_1_View.backgroundColor = [UIColor whiteColor];
    [self.mainScrollView addSubview:self.aboveScroll_1_View];
    [self p_initMainScrollSecondView];
    
    self.aboveScroll_2_View = [[UIView alloc] initWithFrame:CGRectMake(self.mainScrollView.frame.size.width*2, 0, self.mainScrollView.frame.size.width, self.mainScrollView.frame.size.height)];
    self.aboveScroll_2_View.backgroundColor = [UIColor whiteColor];
    [self.mainScrollView addSubview:self.aboveScroll_2_View];
    [self p_initMainScrollThirdView];
    
    self.mainScrollView.contentSize = CGSizeMake(self.view.frame.size.width*3, self.mainScrollView.frame.size.height);
    self.mainScrollView.pagingEnabled =YES;
    [self.view addSubview:self.mainScrollView];
}

- (void)p_initMainScrollFirstView {
    UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init];
    self.orderList_0_CollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.aboveScroll_0_View.frame.size.width, self.aboveScroll_0_View.frame.size.height) collectionViewLayout:flowLayout];
    self.orderList_0_CollectionView.backgroundColor = [UIColor whiteColor];
    self.orderList_0_CollectionView.delegate = self;
    self.orderList_0_CollectionView.dataSource = self;
    self.orderList_0_CollectionView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    //设置contentInset属性（上左下右的值）
    self.orderList_0_CollectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.orderList_0_CollectionView.backgroundColor = [UIColor whiteColor];
    [self.aboveScroll_0_View addSubview:self.orderList_0_CollectionView];
}

- (void)p_initMainScrollSecondView {
    UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init];
    self.orderList_1_CollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.aboveScroll_1_View.frame.size.width, self.aboveScroll_1_View.frame.size.height) collectionViewLayout:flowLayout];
    self.orderList_1_CollectionView.backgroundColor = [UIColor whiteColor];
    self.orderList_1_CollectionView.delegate = self;
    self.orderList_1_CollectionView.dataSource = self;
    //设置contentInset属性（上左下右的值）
    self.orderList_1_CollectionView.contentInset = UIEdgeInsetsMake(15, 15, 15, 15);
    self.orderList_1_CollectionView.backgroundColor = [UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1];
    [self.aboveScroll_1_View addSubview:self.orderList_1_CollectionView];
}

- (void)p_initMainScrollThirdView {
    UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init];
    self.orderList_2_CollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.aboveScroll_2_View.frame.size.width, self.aboveScroll_2_View.frame.size.height) collectionViewLayout:flowLayout];
    self.orderList_2_CollectionView.backgroundColor = [UIColor whiteColor];
    self.orderList_2_CollectionView.delegate = self;
    self.orderList_2_CollectionView.dataSource = self;
    self.orderList_2_CollectionView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    //设置contentInset属性（上左下右的值）
    self.orderList_2_CollectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.orderList_2_CollectionView.backgroundColor = [UIColor whiteColor];
    [self.aboveScroll_2_View addSubview:self.orderList_2_CollectionView];
}

-(void)fromMianPage {
    self.segmentItem_0_BackgroundView.hidden = YES;
    self.segmentItem_1_BackgroundView.hidden = NO;
    self.segmentItem_2_BackgroundView.hidden = YES;
    self.mainScrollView.contentOffset = CGPointMake(self.mainScrollView.frame.size.width, 0);
}

#pragma mark - Private Action
- (void)rightItemAction:(UIButton *)item {
    NSLog(@"right item clicked");
}

- (void)segmentAction:(UIButton *)item {
    if (0 == item.tag) {
        self.segmentItem_0_BackgroundView.hidden = NO;
        self.segmentItem_1_BackgroundView.hidden = YES;
        self.segmentItem_2_BackgroundView.hidden = YES;
        self.mainScrollView.contentOffset = CGPointMake(0, 0);
    }
    else if (1 == item.tag) {
        self.segmentItem_0_BackgroundView.hidden = YES;
        self.segmentItem_1_BackgroundView.hidden = NO;
        self.segmentItem_2_BackgroundView.hidden = YES;
        self.mainScrollView.contentOffset = CGPointMake(self.mainScrollView.frame.size.width, 0);
    }
    else {
        self.segmentItem_0_BackgroundView.hidden = YES;
        self.segmentItem_1_BackgroundView.hidden = YES;
        self.segmentItem_2_BackgroundView.hidden = NO;
        self.mainScrollView.contentOffset = CGPointMake(self.mainScrollView.frame.size.width*2, 0);
    }
    
    //主动切换取消主页进入状态
    [[NSUserDefaults standardUserDefaults] setValue:@(NO) forKey:@"isFromMianPage"];
}

#pragma mark - UIScrollViewDelegate
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    //主动切换取消主页进入状态
    [[NSUserDefaults standardUserDefaults] setValue:@(NO) forKey:@"isFromMianPage"];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat x = scrollView.contentOffset.x;
    _page = x/scrollView.frame.size.width;
    if (_page==0) {
        self.segmentItem_0_BackgroundView.hidden = NO;
        self.segmentItem_1_BackgroundView.hidden = YES;
        self.segmentItem_2_BackgroundView.hidden = YES;
    }
    else if (_page == 1) {
        self.segmentItem_0_BackgroundView.hidden = YES;
        self.segmentItem_1_BackgroundView.hidden = NO;
        self.segmentItem_2_BackgroundView.hidden = YES;
    }
    else {
        self.segmentItem_0_BackgroundView.hidden = YES;
        self.segmentItem_1_BackgroundView.hidden = YES;
        self.segmentItem_2_BackgroundView.hidden = NO;
    }
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 4;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([collectionView isEqual:_orderList_0_CollectionView]) {
        static NSString *ID = @"OrderNewCellIdentifer";
        BCOrderNewCollectionViewCell *cell = [BCOrderNewCollectionViewCell cellWithCollectionView:collectionView];
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        if (indexPath.row == 0) {
            [cell.priceButton setBackgroundImage:[UIImage imageNamed:@"等待竞价"] forState:UIControlStateNormal];
            cell.typeLable.hidden = YES;
            cell.waitTitleLable.hidden = NO;
            cell.timestampLable.hidden = NO;
            cell.type = OrderTypeEnumWait;
        }
        else if (indexPath.row == 1) {
            [cell.priceButton setBackgroundImage:[UIImage imageNamed:@"竞价中"] forState:UIControlStateNormal];
            cell.typeLable.hidden = NO;
            cell.typeLable.text = @"竞价中";
            cell.typeLable.textColor = [UIColor colorWithRed:0.365 green:0.773 blue:0.408 alpha:1.000];
            cell.waitTitleLable.hidden = YES;
            cell.timestampLable.hidden = YES;
            cell.type = OrderTypeEnumAuction;
        }
        else if (indexPath.row == 2) {
            [cell.priceButton setBackgroundImage:[UIImage imageNamed:@"成功失败"] forState:UIControlStateNormal];
            cell.typeLable.hidden = NO;
            cell.typeLable.text = @"竞价失败";
            cell.typeLable.textColor = [UIColor colorWithRed:0.933 green:0.341 blue:0.282 alpha:1.000];
            cell.waitTitleLable.hidden = YES;
            cell.timestampLable.hidden = YES;
            cell.type = OrderTypeEnumFailed;
        }
        else {
            [cell.priceButton setBackgroundImage:[UIImage imageNamed:@"成功失败"] forState:UIControlStateNormal];
            cell.typeLable.hidden = NO;
            cell.typeLable.text = @"竞价成功";
            cell.typeLable.textColor = [UIColor colorWithRed:0.000 green:0.420 blue:0.984 alpha:1.000];
            cell.waitTitleLable.hidden = YES;
            cell.timestampLable.hidden = YES;
            cell.type = OrderTypeEnumSuccess;
        }
        
        return cell;
    }
    else if ([collectionView isEqual:_orderList_1_CollectionView]) {
        static NSString *ID = @"OrderWaitCellIdentifer";
        BCOrderWaitCollectionViewCell *cell = [BCOrderWaitCollectionViewCell cellWithCollectionView:collectionView];
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        return cell;
    }
    else {
        static NSString *ID = @"OrderSendCellIdentifer";
        BCOrderSendCollectionViewCell *cell = [BCOrderSendCollectionViewCell cellWithCollectionView:collectionView];
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        return cell;
    }
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{
    UIEdgeInsets top = {0,0,0,0};
    return top;
}

//定义每个UICollectionViewCell 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([collectionView isEqual:_orderList_0_CollectionView]) {
        return CGSizeMake(self.view.frame.size.width, 108);
    }
    else if ([collectionView isEqual:_orderList_1_CollectionView]) {
        return CGSizeMake(self.view.frame.size.width-30.f, 126);
    }
    else {
        return CGSizeMake(self.view.frame.size.width, 88);
    }
}

//每个item之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

//每个section中不同的行之间的行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    if ([collectionView isEqual:_orderList_0_CollectionView]) {
        return 0;
    }
    else if ([collectionView isEqual:_orderList_1_CollectionView]) {
        return 15;
    }
    else {
        return 0;
    }
}

//collectionview点击
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([collectionView isEqual:_orderList_0_CollectionView]) {
        BCOrderNewCollectionViewCell *cell = (BCOrderNewCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
        BCNewOrdDetailVC *nextViewController = [[BCNewOrdDetailVC alloc] init];
        nextViewController.type = cell.type;
        [self.navigationController pushViewController:nextViewController animated:YES];
    }
    else if ([collectionView isEqual:_orderList_1_CollectionView]) {
        BCReadyDetailVC *nextViewController = [[BCReadyDetailVC alloc] init];
        [self.navigationController pushViewController:nextViewController animated:YES];
    }
    else {
        BCAlreadyDetailVC *nextViewController = [[BCAlreadyDetailVC alloc] init];
        [self.navigationController pushViewController:nextViewController animated:YES];
    }
}

@end
